package com.cardone.platform.common.controller;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cardone.persistent.CommonDtoAttributes;
import com.cardone.platform.authority.dto.NavigationDto;
import com.cardone.platform.authority.service.AuthorityService;
import com.cardone.platform.configuration.service.ConfigurationService;

/**
 * @author yaoht
 */
@Controller
public class IndexController {
	/**
	 * 权限服务
	 */
	@Qualifier(AuthorityService.BEAN_ID)
	@Autowired(required = false)
	private AuthorityService authorityService;

	/**
	 * 配置服务
	 */
	@Qualifier(ConfigurationService.BEAN_ID)
	@Autowired(required = false)
	private ConfigurationService configurationService;

	/**
	 * 首页
	 * 
	 * @return 首页模板文件路径
	 */
	@RequestMapping(value = { "/index.html" }, method = RequestMethod.GET)
	public String index() {
		return "/index";
	}

	/**
	 * 首页左侧
	 * 
	 * @param navigation
	 * @param model
	 * @return 首页左侧模板文件路径
	 */
	@RequestMapping(value = { "/index/left.html" }, method = RequestMethod.POST)
	public String indexLeft(final NavigationDto findListNavigation, final Model model) {
		List<NavigationDto> navigationList = this.authorityService.getNavigationService().findListNavigationByParentId(
		        findListNavigation.getParentId(), 2);

		model.addAttribute("navigationList", navigationList);

		return "/index/left";
	}

	/**
	 * 首页顶部
	 * 
	 * @param navigation
	 * @param model
	 * @return 首页顶部模板文件路径
	 */
	@RequestMapping(value = { "/index/top.html" })
	public String indexTop(final NavigationDto findListNavigation, final Model model) {
		List<NavigationDto> navigationList = this.authorityService.getNavigationService().findListNavigationByParentId(
		        findListNavigation.getParentId());

		model.addAttribute("navigationList", navigationList);

		if (StringUtils.isNotBlank(findListNavigation.getId())) {
			model.addAttribute(CommonDtoAttributes.id.name(), findListNavigation.getId());
		} else {
			if (!CollectionUtils.isEmpty(navigationList)) {
				model.addAttribute(CommonDtoAttributes.id.name(), navigationList.get(0).getId());
			}
		}

		return "/index/top";
	}
}